﻿using WpfWordsLearner;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Collections.Generic;

namespace TestProject
{


	/// <summary>
	///This is a test class for VoiceManagerTest and is intended
	///to contain all VoiceManagerTest Unit Tests
	///</summary>
	[TestClass()]
	public class VoiceManagerTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		/// <summary>
		///A test for Dispose
		///</summary>
		[TestMethod()]
		public void DisposeTest()
		{
			VoiceManager target = new VoiceManager(); // TODO: Initialize to an appropriate value
			target.Dispose();
		}

		/// <summary>
		///A test for Play
		///</summary>
		[TestMethod()]
		public void PlayTest()
		{
			VoiceManager target = new VoiceManager();
			WpfWordsLearner.DAO.WordsDao dao = new WpfWordsLearner.DAO.WordsDao("d:\\Voices.s3db");

			List<byte[]> data = dao.GetVoices();
			foreach (byte[] audio in data)
			{
				target.Play(audio);
			}
			target.Dispose();
		}

		[TestMethod()]
		public void LoadPlaySave()
		{
			WpfWordsLearner.ServiceReferenceTranslater.TranslateServiceClient client = new WpfWordsLearner.ServiceReferenceTranslater.TranslateServiceClient();
			VoiceManager2 target = new VoiceManager2();
			WpfWordsLearner.AlvasVoiceManager manager2 = new AlvasVoiceManager();

			WpfWordsLearner.DAO.WordsDao dao = new WpfWordsLearner.DAO.WordsDao("d:\\Voices.s3db");
			dao.Initialize();

			WpfWordsLearner.DAO.WordsDaoPlain dao2=new WpfWordsLearner.DAO.WordsDaoPlain( "d:\\Voices.s3db");
			dao2.ClearVoice();

			List<string> words = new List<string>(new string[] { "Hello", "accident", "conveince", "conveinced", "prevails", "insight" });
			List<byte[]> bytesData = new List<byte[]>();

			foreach (string word in words)
			{
				WpfWordsLearner.ServiceReferenceTranslater.VoiceRequest req = new WpfWordsLearner.ServiceReferenceTranslater.VoiceRequest();
				req.OriginalText = word;
				req.ToLanguage = "en";
				var response = client.GetVoice(req);
				if (response != null && response.Data != null)
				{
					dao2.AddVoicedWord(word, response.Data);
					bytesData.Add(response.Data);
				}
			}
			int i = 0;
			List<byte[]> data = dao2.GetVoices();
			foreach (byte[] audio in data)
			{
				Stream sr = new MemoryStream(audio);
				target.Play(sr);
				//manager2.StartPlay(sr);
				sr.Dispose();

				System.Threading.Thread.Sleep(500);

				i++;
			}

			target.Dispose();
			manager2.Dispose();
			client.Close();
			dao2.Dispose();
		}

		[TestMethod()]
		public void LoadPlaySave2()
		{
			WpfWordsLearner.ServiceReferenceTranslater.TranslateServiceClient client = new WpfWordsLearner.ServiceReferenceTranslater.TranslateServiceClient();
			VoiceManager target = new VoiceManager();

			WpfWordsLearner.DAO.WordsDao dao = new WpfWordsLearner.DAO.WordsDao("d:\\Voices.s3db");
			dao.Initialize();

			WpfWordsLearner.DAO.WordsDaoPlain dao2 = new WpfWordsLearner.DAO.WordsDaoPlain("d:\\Voices.s3db");
			dao2.ClearVoice();

			List<string> words = new List<string>(new string[] { "Hello", "accident", "conveince", "conveinced", "prevails", "insight" });
			List<byte[]> bytesData = new List<byte[]>();

			foreach (string word in words)
			{
				WpfWordsLearner.ServiceReferenceTranslater.VoiceRequest req = new WpfWordsLearner.ServiceReferenceTranslater.VoiceRequest();
				req.OriginalText = word;
				req.ToLanguage = "en";
				var response = client.GetVoice(req);
				if (response != null && response.Data != null)
				{
					dao2.AddVoicedWord(word, response.Data);
					bytesData.Add(response.Data);
				}
			}
			int i = 0;
			List<byte[]> data = dao2.GetVoices();
			foreach (byte[] audio in data)
			{
				Stream sr = new MemoryStream(audio);
				target.Play(sr);
				//manager2.StartPlay(sr);
				sr.Dispose();

				System.Threading.Thread.Sleep(500);

				i++;
			}

			target.Dispose();
			client.Close();
			dao2.Dispose();
		}
	}
}
